package com.xuecheng.controller;

import com.xuecheng.exception.XueChengPlusException;
import com.xuecheng.model.po.CourseIndex;
import com.xuecheng.service.IndexService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 课程索引接口
 */
@Slf4j
@Api(value = "课程信息索引接口", tags = "课程信息索引接口")
@RestController
@RequestMapping("/index")
public class CourseIndexController {
	
	@Resource
	private IndexService indexService;
	@Value("${elasticsearch.course.index}")
	private String courseIndexStore;
	
	
	@ApiOperation("添加课程索引")
	@PostMapping("course")
	public Boolean add(@RequestBody CourseIndex courseIndex) {
		log.info("添加课程索引：{}", courseIndex);
		Long id = courseIndex.getId();
		if (id == null) {
			XueChengPlusException.cast("课程id为空");
		}
		Boolean result = indexService.addCourseIndex(courseIndexStore, String.valueOf(id), courseIndex);
		if (!result) {
			XueChengPlusException.cast("添加课程索引失败");
		}
		return true;
	}
}
